﻿using System;
using System.Web;
using System.Web.SessionState;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web.Security;
using Gift.DAL;
using Gift.Model;

using Gift.Utile;

namespace WebSite.cn.ashx
{
    public class isAddToAccountHandler : IHttpHandler, IRequiresSessionState
    {
        readonly DALGoods theGoods = new DALGoods();
        readonly DALCar theCar = new DALCar();
        tbGoods tempGoods;
        DB_Car oCar;
        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            BasePage thePage = new BasePage();

            if (context.Request["deleteid"] != null)
            {
                theCar.Delete(int.Parse(context.Request["deleteid"]));
                context.Response.Write("1003");
                return;

            }

            if (context.Request["updateid"] != null)
            {
                oCar = theCar.GetModel(int.Parse(context.Request["updateid"]));
                if (oCar != null)
                {
                    oCar.goodsCount = oCar.goodsCount + int.Parse(context.Request["count"]);
                    if (oCar.goodsCount > 0)
                    {
                        oCar.sumPoints = oCar.goodsCount * oCar.goodsPoints;
                        theCar.Update(oCar);
                        context.Response.Write("1003");
                        return;
                    }
                }
            }

          

         
            tbCardType oCardType = new tbCardType();
            DALCardType theCardType = new DALCardType();

            string sGid = context.Request["gid"];
            string sPoints = context.Request["price"]; 
            string sContent = context.Request["content"]; 
            string sGoodsCount = context.Request["pronum"];
            string sUseWay = context.Request["useway"];
            int iModelTypeId = 0;

            if (!string.IsNullOrEmpty(context.Request["modeltypeid"]))
            {
                iModelTypeId = int.Parse(context.Request["modeltypeid"].Replace("#model",""));
            }

            if (thePage.LoginUserMember == null)
            {
                context.Response.Write("1002");
                return;
            }

            tbGoods oGoods;
            oGoods = theGoods.GetModel(int.Parse(sGid));


            bool isAdd = true;

            if (sUseWay == "1")
            {
                if (!thePage.LoginUserMember.IsCard || thePage.LoginUserMember.DicUserCardList == null || thePage.LoginUserMember.DicUserCardList.Count <=0)
                {
                    context.Response.Write("1002");
                    return;
                }
             
                re.WriteCookie("tempnoCard", thePage.LoginUserMember.TempMembertId, 30);
                oCar = theCar.GetModel(int.Parse(sGid), long.Parse(thePage.LoginUserMember.TempMembertId), true,iModelTypeId);
            }
            else
            {

                if (thePage.LoginUserMember.MemberId == "")
                {
                    context.Response.Write("1002");
                    return;
                }
                 re.WriteCookie("tempnoGenerate", thePage.LoginUserMember.MemberId, 30);
                 oCar = theCar.GetModel(int.Parse(sGid), long.Parse(thePage.LoginUserMember.MemberId), false,iModelTypeId);
            }
            if (oCar != null)
            {
                isAdd = false;
          
                oCar.goodsCount = oCar.goodsCount + int.Parse(sGoodsCount);
            }
            else
            {
                oCar = new DB_Car();
                oCar.goodsCount =  int.Parse(sGoodsCount);
            }

            Hashtable htTemplate = new Hashtable();
            if (sUseWay == "1")
            {
                oCar.memberid = long.Parse(thePage.LoginUserMember.TempMembertId);
                DataTable tbCar = theCar.GetListByMemberId(long.Parse(thePage.LoginUserMember.TempMembertId), 1).Tables[0];
                foreach (DataRow dr in tbCar.Rows)
                {
                    if (htTemplate[dr["deliveryTemplateId"].ToString()] != null)
                    {
                        context.Response.Write("1003");
                        return;
                    }
                    else
                    {
                        htTemplate.Add(dr["deliveryTemplateId"].ToString(), dr["deliveryTemplateId"].ToString());
                    }
                }

            }
            else
            {
                oCar.memberid = long.Parse(thePage.LoginUserMember.MemberId);
                DataTable tbCar = theCar.GetListByMemberId(long.Parse(thePage.LoginUserMember.MemberId), 0).Tables[0];
                foreach (DataRow dr in tbCar.Rows)
                {
                    if (htTemplate[dr["deliveryTemplateId"].ToString()] != null)
                    {
                        foreach (string key in htTemplate.Keys)
                        {
                            if (key != dr["deliveryTemplateId"].ToString())
                            {
                                context.Response.Write("1003");
                                return;
                            }
                        }
                    }
                    else
                    {
                        htTemplate.Add(dr["deliveryTemplateId"].ToString(), dr["deliveryTemplateId"].ToString());
                    }
                }
            }
      
          
            oCar.goodsId = int.Parse(sGid);
            oCar.isCard = sUseWay == "1" ? true : false;
            oCar.state = false;
            oCar.flag = false;
            oCar.goodsPoints = int.Parse(sPoints);
            oCar.contents = sContent;
            oCar.sumPoints = oCar.goodsCount * int.Parse(sPoints);
            oCar.ModelTypeId = iModelTypeId;

   
            if (isAdd)
            {
                if (oCar.goodsCount > 0)
                {
                    if (oCar.goodsPoints > 0)
                    {
                        theCar.Add(oCar);
                    }
                }
            }
            else
            {
                if (oCar.goodsCount > 0)
                {
                    theCar.Update(oCar);
                }
             
            }
           

        }

        #endregion
    }
}
